home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Demo / tkinter / guido / tkman.py < prev    next >
Encoding:
Python Source  |  1996-08-02  |  6.3 KB  |  240 lines  |  [TEXT/Pyth]

  1. #! /usr/local/bin/python
  2.  
  3. # Tk man page browser -- currently only shows the Tcl/Tk man pages
  4.  
  5. import sys
  6. import os
  7. import string
  8. import regex
  9. from Tkinter import *
  10.  
  11. import addpack
  12. addpack.addpack('/ufs/guido/src/python/Demo/guido/tkinter')
  13. from ManPage import ManPage
  14.  
  15. MANNDIR = '/usr/local/man/mann'
  16. MAN3DIR = '/usr/local/man/man3'
  17. MANNDIR = '/depot/sundry/man/mann'
  18. MAN3DIR = '/depot/sundry/man/man3'
  19.  
  20. def listmanpages(mandir):
  21.     files = os.listdir(mandir)
  22.     names = []
  23.     for file in files:
  24.         if file[-2:-1] == '.' and  (file[-1] in 'ln123456789'):
  25.             names.append(file[:-2])
  26.     names.sort()
  27.     return names
  28.  
  29. class SelectionBox:
  30.  
  31.     def __init__(self, master=None):
  32.         self.choices = []
  33.  
  34.         self.frame = Frame(master, name="frame")
  35.         self.frame.pack(expand=1, fill=BOTH)
  36.         self.master = self.frame.master
  37.         self.subframe = Frame(self.frame, name="subframe")
  38.         self.subframe.pack(expand=0, fill=BOTH)
  39.         self.leftsubframe = Frame(self.subframe, name='leftsubframe')
  40.         self.leftsubframe.pack(side=LEFT, expand=1, fill=BOTH)
  41.         self.rightsubframe = Frame(self.subframe, name='rightsubframe')
  42.         self.rightsubframe.pack(side=RIGHT, expand=1, fill=BOTH)
  43.         self.chaptervar = StringVar(master)
  44.         self.chapter = Menubutton(self.rightsubframe, name='chapter',
  45.                       text='Directory', relief=RAISED,
  46.                       borderwidth=2)
  47.         self.chapter.pack(side=TOP)
  48.         self.chaptermenu = Menu(self.chapter, name='chaptermenu')
  49.         self.chaptermenu.add_radiobutton(label='C functions',
  50.                          value=MAN3DIR,
  51.                          variable=self.chaptervar,
  52.                          command=self.newchapter)
  53.         self.chaptermenu.add_radiobutton(label='Tcl/Tk functions',
  54.                          value=MANNDIR,
  55.                          variable=self.chaptervar,
  56.                          command=self.newchapter)
  57.         self.chapter['menu'] = self.chaptermenu
  58.         self.listbox = Listbox(self.rightsubframe, name='listbox',
  59.                        relief=SUNKEN, borderwidth=2,
  60.                        width=20, height=5)
  61.         self.listbox.pack(expand=1, fill=BOTH)
  62.         self.l1 = Button(self.leftsubframe, name='l1',
  63.                  text='Display manual page named:',
  64.                  command=self.entry_cb)
  65.         self.l1.pack(side=TOP)
  66.         self.entry = Entry(self.leftsubframe, name='entry',
  67.                     relief=SUNKEN, borderwidth=2,
  68.                     width=20)
  69.         self.entry.pack(expand=0, fill=X)
  70.         self.l2frame = Frame(self.leftsubframe, name='l2frame')
  71.         self.l2frame.pack(expand=0, fill=NONE)
  72.         self.l2 = Button(self.l2frame, name='l2',
  73.                  text='Search regexp:',
  74.                  command=self.search_cb)
  75.         self.l2.pack(side=LEFT)
  76.         self.casevar = BooleanVar()
  77.         self.casesense = Checkbutton(self.l2frame, name='casesense',
  78.                          text='Case sensitive',
  79.                          variable=self.casevar,
  80.                          relief=FLAT)
  81.         self.casesense.pack(side=LEFT)
  82.         self.search = Entry(self.leftsubframe, name='search',
  83.                     relief=SUNKEN, borderwidth=2,
  84.                     width=20)
  85.         self.search.pack(expand=0, fill=X)
  86.         self.title = Label(self.leftsubframe, name='title',
  87.                    text='(none)')
  88.         self.title.pack(side=BOTTOM)
  89.         self.text = ManPage(self.frame, name='text',
  90.                     relief=SUNKEN, borderwidth=2,
  91.                     wrap=NONE, width=72,
  92.                     selectbackground='pink')
  93.         self.text.pack(expand=1, fill=BOTH)
  94.  
  95.         self.entry.bind('<Return>', self.entry_cb)
  96.         self.search.bind('<Return>', self.search_cb)
  97.         self.listbox.bind('<Double-1>', self.listbox_cb)
  98.  
  99.         self.entry.bind('<Tab>', self.entry_tab)
  100.         self.search.bind('<Tab>', self.search_tab)
  101.         self.text.bind('<Tab>', self.text_tab)
  102.  
  103.         self.entry.focus_set()
  104.  
  105.         self.chaptervar.set(MANNDIR)
  106.         self.newchapter()
  107.  
  108.     def newchapter(self):
  109.         mandir = self.chaptervar.get()
  110.         self.choices = []
  111.         self.addlist(listmanpages(mandir))
  112.  
  113.     def addchoice(self, choice):
  114.         if choice not in self.choices:
  115.             self.choices.append(choice)
  116.             self.choices.sort()
  117.         self.update()
  118.  
  119.     def addlist(self, list):
  120.         self.choices[len(self.choices):] = list
  121.         self.choices.sort()
  122.         self.update()
  123.  
  124.     def entry_cb(self, *e):
  125.         self.update()
  126.  
  127.     def listbox_cb(self, e):
  128.         selection = self.listbox.curselection()
  129.         if selection and len(selection) == 1:
  130.             name = self.listbox.get(selection[0])
  131.             self.show_page(name)
  132.  
  133.     def search_cb(self, *e):
  134.         self.search_string(self.search.get())
  135.  
  136.     def entry_tab(self, e):
  137.         self.search.focus_set()
  138.  
  139.     def search_tab(self, e):
  140.         self.entry.focus_set()
  141.  
  142.     def text_tab(self, e):
  143.         self.entry.focus_set()
  144.  
  145.     def updatelist(self):
  146.         key = self.entry.get()
  147.         ok = filter(lambda name, key=key, n=len(key): name[:n]==key,
  148.              self.choices)
  149.         if not ok:
  150.             self.frame.bell()
  151.         self.listbox.delete(0, AtEnd())
  152.         exactmatch = 0
  153.         for item in ok:
  154.             if item == key: exactmatch = 1
  155.             self.listbox.insert(AtEnd(), item)
  156.         if exactmatch:
  157.             return key
  158.         n = self.listbox.size()
  159.         if n == 1:
  160.             return self.listbox.get(0)
  161.         # Else return None, meaning not a unique selection
  162.  
  163.     def update(self):
  164.         name = self.updatelist()
  165.         if name:
  166.             self.show_page(name)
  167.             self.entry.delete(0, AtEnd())
  168.             self.updatelist()
  169.  
  170.     def show_page(self, name):
  171.         file = '%s/%s.?' % (self.chaptervar.get(), name)
  172.         fp = os.popen('nroff -man %s | ul -i' % file, 'r')
  173.         self.text.kill()
  174.         self.title['text'] = name
  175.         self.text.parsefile(fp)
  176.  
  177.     def search_string(self, search):
  178.         if not search:
  179.             self.frame.bell()
  180.             print 'Empty search string'
  181.             return
  182.         if not self.casevar.get():
  183.             map = regex.casefold
  184.         else:
  185.             map = None
  186.         try:
  187.             if map:
  188.                 prog = regex.compile(search, map)
  189.             else:
  190.                 prog = regex.compile(search)
  191.         except regex.error, msg:
  192.             self.frame.bell()
  193.             print 'Regex error:', msg
  194.             return
  195.         here = self.text.index(AtInsert())
  196.         lineno = string.atoi(here[:string.find(here, '.')])
  197.         end = self.text.index(AtEnd())
  198.         endlineno = string.atoi(end[:string.find(end, '.')])
  199.         wraplineno = lineno
  200.         found = 0
  201.         while 1:
  202.             lineno = lineno + 1
  203.             if lineno > endlineno:
  204.                 if wraplineno <= 0:
  205.                     break
  206.                 endlineno = wraplineno
  207.                 lineno = 0
  208.                 wraplineno = 0
  209.             line = self.text.get('%d.0 linestart' % lineno,
  210.                          '%d.0 lineend' % lineno)
  211.             i = prog.search(line)
  212.             if i >= 0:
  213.                 found = 1
  214.                 n = max(1, len(prog.group(0)))
  215.                 try:
  216.                     self.text.tag_remove('sel',
  217.                                  AtSelFirst(),
  218.                                  AtSelLast())
  219.                 except TclError:
  220.                     pass
  221.                 self.text.tag_add('sel',
  222.                           '%d.%d' % (lineno, i),
  223.                           '%d.%d' % (lineno, i+n))
  224.                 self.text.mark_set(AtInsert(),
  225.                            '%d.%d' % (lineno, i))
  226.                 self.text.yview_pickplace(AtInsert())
  227.                 break
  228.         if not found:
  229.             self.frame.bell()
  230.  
  231. def main():
  232.     root = Tk()
  233.     sb = SelectionBox(root)
  234.     if sys.argv[1:]:
  235.         sb.show_page(sys.argv[1])
  236.     root.minsize(1, 1)
  237.     root.mainloop()
  238.  
  239. main()
  240.